import { createSlice } from '@reduxjs/toolkit'
import {setTokenFun, getToken, removeToken} from '@/utils/utils'

const userStore = createSlice({
  name: 'user',
  initialState: {
    token: getToken() || '',
  },
  reducers: {
    setToken(state: any, action) {
      state.token = action.payload
      setTokenFun(action.payload)
    },
  }
})

const fetchLogin = (loginFrom: any) => {
  return (dispatch: any) => {
    if (loginFrom) {
      const resToken = 'uyGDCCUYajvuaKJVFUSDAKJH'
      setTimeout(() => {
        dispatch(setToken(resToken))
      }, 1000)
    }
  }
}

const { setToken } = userStore.actions
const userReducer = userStore.reducer

export {setToken, fetchLogin}
export default userReducer